package 二叉树相关问题.层次遍历;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 找到书中每行的最大值 {
    public List<Integer> largestValues(TreeNode root) {
        if (root==null)
            return null;
        List<Integer> list = new LinkedList<>();
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()){
            int max=Integer.MIN_VALUE;
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = q.poll();
                max=Math.max(poll.val,max);
                if (poll.left!=null)
                    q.offer(poll.left);
                if (poll.right!=null)
                    q.offer(poll.right);
            }
            list.add(max);
        }
        return list;
    }
}
